﻿using Nova_BLL;
using Nova_Common;
using Nova_Common.Extend;
using Nova_DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Manage_ReplenShipListV : System.Web.UI.Page
{
    Nova_DAL.DbHelper dbHelper = new DbHelper();
    TCommandsDetailBll CmdDetialBll = null;
    TCommandBll CmdBll = null;
    string[] ExtensionLanelists = new string[] { "01" };

    public Manage_ReplenShipListV()
    {
        CmdDetialBll = new TCommandsDetailBll();
        CmdBll = new TCommandBll();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.hidden.Value != "" && this.hidden.Value != "undefined")
        {
            // gv_Bind();
            gvDateBind();
        }
        if (!IsPostBack)
        {
        }
        gv_Bind();
    }

    protected void ibtn_Search_Click(object sender, ImageClickEventArgs e)
    {
        if (txt_StorageNo.Text.Trim().IsEmpty())
        {
            ShowBox.MessageBox(this.Page, "请填写补货单据号!");
            return;
        }
        gv_Bind();
        gvDateReset();
    }

    public void gv_Bind()
    {
        string sqlStr = @"SELECT TAL.PID,TAL.Sec_Mat_ID,TAL.StockNo,TBM.Mat_Code,TBM.MAT_NAME,TBM.Unit_Base,TBM.TM03,TBM.S02,TBM.TM01,TAL.S05,TBM.GroupType,
                        TAL.Num,TAL.Act_Num,TBM.S01,TAL.TS01,ISNULL(TCD.DETAIL_PID,0) DETAIL_PID  FROM dbo.TAddStorage_List TAL 
                        LEFT JOIN dbo.TBase_Material TBM ON TBM.MAT_CODE=TAL.MAT_CODE
                        LEFT JOIN dbo.TCommandsDetail TCD ON TAL.PID = TCD.DETAIL_PID
                        WHERE TAL.StockNo='" + txt_StorageNo.Text + "' ";
        DbCommand cmd = dbHelper.GetSqlStringCommond(sqlStr);
        gv.DataSource = dbHelper.ExecuteDataSet(cmd);
        gv.DataBind();
    }

    protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex != -1)
        {
            LinkButton button = (LinkButton)e.Row.FindControl("lbtn_Replen");
            string[] sArray = button.CommandArgument.ToString().Split('|');
            string matCode = sArray[0].ToString();
            string secMatId = sArray[1].ToString();
            string num = sArray[2].ToString();
            string actNum = sArray[3].ToString();
            string pid = sArray[4].ToString();
            string groupType = sArray[5].ToString();
            //string str = string.Format("javascript: document.getElementById('{1}').value=window.showModalDialog('SearchTrayInfo.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") +
            //    "&pid={0}&matCode={2}&secMatId={3}&num={4}', window, 'dialogWidth=900px;dialogHeight=530px;scroll:no;resizable:no;status:0;');document.URL=location.href;",
            //   pid, this.hidden.ClientID, matCode, secMatId, num);


            string str = string.Format("javascript:window.open('SearchTrayInfo.aspx?Ram=" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "&pid={0}&matCode={2}&secMatId={3}&num={4}&groupType={5}', window, 'Height=500px,Width=1160px,top=200, left=300, toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, status=no');document.URL=location.href;void(0);",
                pid, this.hidden.ClientID, matCode, secMatId, num, groupType);
            button.Attributes.Add("onclick", str);
            Label lab_DETAIL_PID = (Label)e.Row.FindControl("lab_DETAIL_PID");
            if (decimal.Parse(num) <= decimal.Parse(actNum))
            {
                button.Visible = false;
            }
            // ImageButton ibtnAutoOut = (ImageButton)e.Row.FindControl("ibtnAutoOut");
            if (lab_DETAIL_PID.Text != "0")  //Command_detail表中存在显示红色，表示正在处理
            {
                e.Row.ForeColor = Color.FromName("red");
                //ibtnAutoOut.Visible = false;   //订单明细正在处理的，自动操作按钮隐藏
            }

        }
    }

    private void gvDateBind()
    {
        string hidVal = this.hidden.Value.ToString();
        if (!hidVal.IsEmpty())
        {
            this.hidden.Value = string.Empty;
            string[] strArray = hidVal.Split('|');
            DataTable dt = new DataTable("datas");
            dt.Columns.Add("PID");
            dt.Columns.Add("Mat_Code");
            dt.Columns.Add("Mat_Name");
            dt.Columns.Add("Mat_Num");
            dt.Columns.Add("AddPID");
            dt.Columns.Add("Tray_ID");
            dt.Columns.Add("Pos_ID");
            DataRow row = dt.NewRow();
            row["PID"] = 0;
            row["Mat_Code"] = strArray[0];
            row["Mat_Name"] = strArray[1];
            row["Mat_Num"] = strArray[2];
            row["AddPID"] = strArray[3];
            row["Tray_ID"] = strArray[4];
            row["Pos_ID"] = strArray[5];
            dt.Rows.Add(row);
            gvDate.DataSource = dt;
            gvDate.DataBind();
        }
    }

    protected void ibt_reset_Click(object sender, ImageClickEventArgs e)
    {
        gvDateReset();
    }

    protected void ibtndelete_Click(object sender, ImageClickEventArgs e)
    {
        gvDateReset();
    }

    private void gvDateReset()
    {
        this.hidden.Value = null;
        this.gvDate.DataSource = null;
        this.gvDate.DataBind();
    }

    protected void ibtn_Replen_Click(object sender, ImageClickEventArgs e)
    {
        string userName = Request.Cookies["vNovaUserNo"].Value.ToString();
        if (gvDate.Rows.Count <= 0)
            return;
        Label labTray_ID = (Label)this.gvDate.Rows[0].FindControl("labTray_ID");
        Label labPos_ID = (Label)this.gvDate.Rows[0].FindControl("labPos_ID");
        Label labAddPID = (Label)this.gvDate.Rows[0].FindControl("labAddPID");

        #region 判断双伸位是否可以出库
        //if ((Convert.ToInt32(labPos_ID.Text.Substring(8, 2)) % 2) == 0 && ExtensionLanelists.Contains(labPos_ID.Text.Substring(2, 2)))// 双伸位 后面的货位
        //{
        //    long Pos_ID_Front = (Convert.ToInt64(labPos_ID.Text.Substring(0, 10)) - 1) * 100;
        //    string sqlStr = string.Format(@"select * from  TBase_Position where Pos_ID={0}", Pos_ID_Front);
        //    var cmd = dbHelper.GetSqlStringCommond(sqlStr);
        //    var ds = dbHelper.ExecuteDataSet(cmd);
        //    if (ds.Tables[0].Rows.Count > 0)
        //    {
        //        if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 2)
        //        {
        //            ShowBox.MessageBox(this.Page, string.Format("{0}前面的货位有入库预定，此货位的货物不能出库!", labPos_ID.Text));
        //            return;
        //        }

        //        if (Convert.ToInt16(ds.Tables[0].Rows[0]["Pos_State"]) == 6)
        //        {
        //            ShowBox.MessageBox(this.Page, string.Format("{0}前面的货位有移库预定，此货位的货物不能出库!", labPos_ID.Text));
        //            return;
        //        }

        //    }
        //}

        #endregion

        // 生成离线补货,补货的拣选口只能是左侧带检测门的1001出库口
        int SQID = 1004;
        if (!CmdBll.IsDeviceEnable(SQID))
        {
            Label1.Text = string.Format("{0}出库路径输送线设备、巷道处于禁用或者离线模式!", SQID);
            //ShowBox.MessageBox(this.Page, string.Format("{0}出库路径设备处于禁用或者离线模式!", SQID));
            return;
        }
        string taskId = CmdBll.InsertEntity((int)TaskModeEnum.Replen, labTray_ID.Text, long.Parse(labPos_ID.Text), 200000000000,
             1, SQID, 1, userName);
        if (!taskId.IsEmpty())
        {
            // 插入指令详情
            bool res = CmdDetialBll.InsertEntity(long.Parse(taskId), long.Parse(labAddPID.Text), "0");
            if (res)
            {
                gvDateReset();
                gv_Bind();
                Label1.Text = "生成补货指令成功!";
                //ShowBox.MessageBox(this.Page, "生成补货指令成功!");
            }

        }
    }
}